package com.googlecode.linkmein.domain;

import ymsg.network.Session;
import ymsg.network.YahooUser;

import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.io.IOException;

import static com.googlecode.linkmein.utils.Util.defaultString;
import org.apache.commons.lang.StringUtils;

public class Contact {

    protected String yahooId;
    private static Pattern NAME = Pattern.compile("([a-zA-Z ]*)(.*)");
    protected Name name;
    protected String msn;
    protected String skypeId;
    protected String blog;
    protected String cellphone;
    public static final Contact NULL = new NullContact();
    private String googleId;

    public Contact() {
    }

    public Contact(Name name, String yahooId) {
        this.name = name;
        this.yahooId = yahooId;
    }

    public Contact(String realName, String googleId, String cellphone, String yahooId, String msn, String skypeId, String blog) {
        Matcher matcher = NAME.matcher(realName);
        if (matcher.find()) {
            String englishName = defaultString(matcher.group(1).trim());
            String chineseName = defaultString(matcher.group(2).trim());

            chineseName = chineseName.length() > 2 ?
                    StringUtils.substring(chineseName, 1, chineseName.length() - 1) : chineseName;
            name = new Name(englishName, chineseName);
        }
        this.googleId = googleId;
        this.yahooId = yahooId;
        this.msn = msn;
        this.skypeId = skypeId;
        this.blog = blog;
        this.cellphone = cellphone;
    }

    public Contact(String english, String chinese, String yahooId) {
        this(new Name(english, chinese), yahooId);
    }

    public static Contact get(String realName, String googleId, String cellphone, String yahooId, String msn, String skypeId, String blog) {
        return new Contact(realName, googleId, cellphone, yahooId, msn, skypeId, blog);
    }

    public String toString() {
        return name + " " + yahooId;
    }

    public boolean isSelf(String userName) {
        return userName.equals(yahooId);
    }

    public Boolean isFriend(Session session) {
        YahooUser user = session.getUser(yahooId);
        return user != null && user.isFriend();
    }

    public Result delete(Session session) throws IOException {
        session.removeFriend(yahooId, null);
        return null;
    }

    Result invite(Session session) {
        try {
            session.addFriend(yahooId, "ThoughtWorksChina");
            return new Result(ResultType.SUCCESSFUL, "Successfully invited " + name + "");
        } catch (Exception e) {
            return new Result(ResultType.FAILED, "Failed to invit " + name + "( " + yahooId + ")");
        }
    }


    public Name getName() {
        return name == null ? Name.NULL : name;
    }

    public String getYim() {
        return yahooId;
    }

    public String getMSN() {
        return msn;
    }

    public String getSkype() {
        return skypeId;
    }

    public String getBlog() {
        return blog;
    }

    public String getGoogleId() {
        return this.googleId;
    }

    public String getCellphone() {
        return this.cellphone;
    }

    public String googleIdWithOutGmail() {
        return this.googleId == null ? "" : this.googleId.replaceAll("@.*", "");
    }

    public Contact merge(Contact contact) {
        Name cloned = getName() instanceof NullName ? contact.getName() : getName();
        String clonedYahooId = StringUtils.isEmpty(this.yahooId) ? contact.getYim() : this.yahooId;
        String clonedMSN = StringUtils.isEmpty(this.msn) ? contact.getMSN() : this.msn;
        String blog = StringUtils.isEmpty(this.blog) ? contact.getBlog() : this.blog;
        String skypeId = StringUtils.isEmpty(this.skypeId) ? contact.getSkype() : this.skypeId;
        String googleId = StringUtils.isEmpty(this.googleId) ? contact.getGoogleId() : this.googleId;
        String cellphone = StringUtils.isEmpty(this.cellphone) ? contact.getCellphone() : this.cellphone;
        Contact clonedContact = new Contact(cloned, clonedYahooId);
        clonedContact.msn = clonedMSN;
        clonedContact.blog = blog;
        clonedContact.skypeId = skypeId;
        clonedContact.googleId = googleId;
        clonedContact.cellphone = cellphone;
        return clonedContact;
    }
}